/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.ts.controller;

import java.util.Collection;
import java.util.List;

import com.xy.biz.ts.domain.req.GetTrainRequest;
import com.xy.lang.domain.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.ts.domain.resp.GetTrainFlowHandleResponse;
import com.xy.biz.ts.domain.req.GetTrainFlowHandleRequest;
import com.xy.biz.ts.domain.req.ListTrainFlowHandleRequest;
import com.xy.biz.ts.domain.req.SaveTrainFlowHandleRequest;
import com.xy.biz.ts.domain.query.TrainFlowHandleQuery;
import com.xy.biz.ts.domain.converter.TrainFlowHandleConverter;
import com.xy.biz.ts.client.TrainFlowHandleClient;
import com.xy.biz.ts.service.TrainFlowHandleService;

/**
 *
 *
 * @author zgy
 * @date 2021-6-28
 */
@Api(tags = "培训节点处理")
@Slf4j
@RestController
@RequestMapping("/api/ts/train_handle")
public class TrainFlowHandleController implements TrainFlowHandleClient {

    @Autowired
    private TrainFlowHandleService trainFlowHandleService;

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> saveTrainFlowHandle(@RequestBody SaveTrainFlowHandleRequest request) {
        trainFlowHandleService.saveTrainFlowHandle(TrainFlowHandleConverter.toBO(request));
        return Result.success();
    }

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_all")
    public Result<?> saveAllTrainFlowHandle(@RequestBody Collection<SaveTrainFlowHandleRequest> collection) {
        trainFlowHandleService.saveAllTrainFlowHandle(TrainFlowHandleConverter.toBO(collection));
        return Result.success();
    }

    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updateTrainFlowHandle(@RequestBody SaveTrainFlowHandleRequest request) {
        trainFlowHandleService.updateTrainFlowHandle(TrainFlowHandleConverter.toBO(request));
        return Result.success();
    }

    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteTrainFlowHandle(@RequestBody GetTrainFlowHandleRequest request) {
        trainFlowHandleService.deleteTrainFlowHandle(request.getTrainFlowHandleId());
        return Result.success();
    }

    @Validatable
    @PostMapping(value = "/get")
    @Override
    public Result<GetTrainFlowHandleResponse> getTrainFlowHandle(@RequestBody GetTrainFlowHandleRequest request) {
        return Result.success(TrainFlowHandleConverter.toResponse(trainFlowHandleService.getTrainFlowHandle(request.getTrainFlowHandleId())));
    }

    @PostMapping(value = "/list_by_ids")
    @Override
    public Result<List<GetTrainFlowHandleResponse>> listTrainFlowHandleById(@RequestBody Collection<Long> collection) {
        return Result.success(TrainFlowHandleConverter.toResponse(trainFlowHandleService.listTrainFlowHandleById(collection)));
    }

    @ApiOperation("培训/培训总结处理过的节点数据")
    @PostMapping(value = "/list_node")
    @Validatable
    public Result<List<GetTrainFlowHandleResponse>> listTrainFlowNode(@RequestBody GetTrainRequest request) {
        return trainFlowHandleService.listTrainFlowNode(request);
    }

    @PostMapping(value = "/list")
    @Override
    public Result<List<GetTrainFlowHandleResponse>> listTrainFlowHandle(@RequestBody ListTrainFlowHandleRequest request) {
        return Result.success(TrainFlowHandleConverter.toResponse(trainFlowHandleService.listTrainFlowHandle(TrainFlowHandleConverter.toQuery(request))));
    }

    @PostMapping(value = "/count")
    @Override
    public Result<Integer> countTrainFlowHandle(@RequestBody ListTrainFlowHandleRequest request) {
        return Result.success(trainFlowHandleService.countTrainFlowHandle(TrainFlowHandleConverter.toQuery(request)));
    }

    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetTrainFlowHandleResponse>> pageTrainFlowHandle(@RequestBody ListTrainFlowHandleRequest request) {
        TrainFlowHandleQuery query = TrainFlowHandleConverter.toQuery(request);
        List<GetTrainFlowHandleResponse> list = TrainFlowHandleConverter.toResponse(trainFlowHandleService.listTrainFlowHandle(query));
        int count = trainFlowHandleService.countTrainFlowHandle(query);
        return Result.success(new Page<>(list, count));
    }

}
